import { Component } from '@angular/core';
import { NavController,ToastController } from 'ionic-angular';

import { Storage } from '@ionic/storage';
import { ApiService } from '../../providers/ApiService';

@Component({
  selector: 'page-modifypassword',
  templateUrl: 'modifypassword.html'
})
export class ModifyPassword {
  form_content = {
    token:'',
    password:'',
    newpassword:'',
    renewpassword:''
  }
  constructor(
    public navCtrl: NavController,
    public toastCtrl: ToastController,
    public storage: Storage,
    private apiService: ApiService
  ){
    this.storage.get('mytoken').then((mytoken)=>{
      if(mytoken) {
        this.form_content.token = mytoken;
      }
    })
  }
  dismissHandler() {
    //console.info('Toast onDidDismiss()');
  }
  //消息提示函数
  formTip(hint){
    const toast = this.toastCtrl.create({
      message: hint,
      duration: 3000,
      position: 'middle'
    });
    toast.onDidDismiss(this.dismissHandler);
    toast.present();
    return;
  }
  checkIdentical(){
    if(this.form_content.newpassword != this.form_content.renewpassword){
      this.formTip('两次密码不一样');
    }
  }
  modifyPassword(){
    if(this.form_content.password == ''||this.form_content.newpassword == '' || this.form_content.renewpassword == ''){
      this.formTip('密码或重复密码不能为空');
      return;
    }
    if(this.form_content.newpassword != this.form_content.renewpassword){
      this.formTip('两次密码不一样');
    }
    this.apiService.modifyPasswords(this.form_content,response => {
      this.formTip(response.json().info);
    })
  }
}
